***************************************************
/* This do-file replicates Figure 3 of the paper */
***************************************************

set more off 

cd "" /*input directory here with cleaned data south_africa_GHS using the ad hoc GHS do-file */

use south_africa_GHS.dta, clear 

bys household_id year: egen grant_h=max(child_grant)  /* variable at the household level equal to 1 if anybody in the household receives the grant */ 


gen cohort= year-age /* No Information for cohort, given that GHS takes place in July, year - age gives a cohort born from july of t-1 to june of t. See paper for detailed explanation */  

drop if  race==3 | race==4 /* Exclude population groups that do not receive CSG to a significant extent, see text for explanation */ 



*********************************************
* CSG take- up by youngest cohort and year ** 
*********************************************

* Merge Child-Level information on grant with recipient mothers * 

preserve 

keep household_id cohort mother_id child_grant age year

rename mother_id id 
rename cohort cohort_child 
rename child_grant grant_child
rename age age_child 

sort household_id id  

save children_GHS.dta, replace

restore 

merge 1:m household_id year id using children_GHS.dta 

erase children_GHS.dta

keep if _merge==3 /* this only keeps mother with children observed in the household */ 

keep if sex==2 /* Excludes a few mistmatched observations (matched with man, <1%) */ 
drop if age<age_child+15 /* Excludes if age of the child is too young wrt to mother (1%) */ 


* Keep only the youngest child for each mother * 

egen id_mother=group(household_id id year)

drop child_grant 

bys id_mother: egen youngest_child_cohort=max(cohort_child) /* for each individual mother, identify the youngest child */ 

keep if cohort_child==youngest_child_cohort /* keeps only observations for the youngest.  */ 

bys id_mother: egen child_grant_mother=max(grant_child) /* This defines grant receipt at the mother level. Nothing changes if for the mother has only one 
youngest child. If the mother has two youngest children of the same cohort (roughly 3% of the sample), it creates a variable equal to 1 if any of the two children receives the child support grant  */


keep if cohort_child>=1991 & cohort_child<=1995 /* Relevant cohorts */

duplicates drop id_mother, force /*keep only one observation per mother */ 




****************************
** First Stage Estimation ** 
****************************

drop if year==2002 /*exclude 2002 because in this year CSG information is only coded at the household level */ 

keep if cohort_child>=1992 & cohort_child<=1993 /* Relevant cohorts */

gen CSG=(cohort_child==1993) 

gen coef=.
gen ub=. 
gen lb=. 



forvalues i= 2003 (1) 2011 {

reg child_grant_mother 1.CSG if year==`i' [w=weight], cluster(household_id)
replace coef=_b[1.CSG] if year==`i'
replace ub=_b[1.CSG] + _se[1.CSG]*1.96 if year==`i'
replace lb=_b[1.CSG] - _se[1.CSG]*1.96 if year==`i'

}

egen tag=tag(year)

************
*** Fig 3 **
************ 

twoway (scatter coef year if tag==1, mcolor(black)) || (rcap ub lb year, lcolor(black)) ///
||,  graphregion( color(white) ) plotregion(  fcolor(white) ) xlabel(2003 (1) 2011) ///
||, legend(off) xtitle("") ylabel( -0.1 (0.1) 0.4)


